package com.example.pharmacy.service;

import com.example.pharmacy.mapper.DrugInfoMapper;
import com.example.pharmacy.mapper.OrderInfoMapper;
import com.example.pharmacy.mapper.OrderItemMapper;
import com.example.pharmacy.model.DrugInfo;
import com.example.pharmacy.model.OrderInfo;
import com.example.pharmacy.model.OrderItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: asus
 * Date: 2024-12-22
 * Time: 19:21
 */
@Service
public class OrderItemService {
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private DrugInfoMapper drugInfoMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    public Integer updateOrderItem(OrderItem orderItem) {
        if (orderItem.getQuantity() != null) {
            //更新药品库存
            Integer newQuantity = orderItem.getQuantity();
            OrderItem oldOrderItem = orderItemMapper.selectOrderItemById(orderItem.getOrderItemId());
            Integer oldQuantity = oldOrderItem.getQuantity();
            int diff = newQuantity - oldQuantity;
            DrugInfo drugInfo = drugInfoMapper.selectDrugById(oldOrderItem.getDrugId());
            int newInventory = drugInfo.getInventory() - diff;
            if (newInventory < 0) {
                return 0;
            }
            drugInfo.setDrugId(orderItem.getDrugId());
            drugInfo.setInventory(newInventory);
            drugInfoMapper.updateDrug(drugInfo);
            //更新订单金额
            OrderInfo orderInfo = orderInfoMapper.getOrderInfoByOrderId(oldOrderItem.getOrderId());
            BigDecimal oldAmount = orderInfo.getTotalAmount();
            BigDecimal newAmount = oldAmount.add(drugInfo.getPrice().multiply(new BigDecimal(diff)));
            orderInfo.setTotalAmount(newAmount);
            orderInfoMapper.updateOrder(orderInfo);
        }
        return orderItemMapper.updateOrderItem(orderItem);
    }

    /**
     * 添加新订单的订单项
     * @param orderItems
     * @return 0: 该药品没有库存
     */
    public Integer addOrderItems(List<OrderItem> orderItems) {
        BigDecimal amount = new BigDecimal(0);
        Integer orderId = 0;
        for (OrderItem orderItem : orderItems) {
            orderId = orderItem.getOrderId();
            Integer drugId = orderItem.getDrugId();
            Integer num = orderItem.getQuantity();
            DrugInfo drugInfo = drugInfoMapper.selectDrugById(drugId);
            DrugInfo oldDrugInfo = drugInfoMapper.selectDrugById(drugId);
            Integer oldNum = oldDrugInfo.getInventory();
            int newNum = oldNum - num;
            if (newNum < 0) {
                return 0;
            }
            drugInfo.setInventory(newNum);
            drugInfoMapper.updateDrug(drugInfo);
            amount = amount.add(drugInfo.getPrice().multiply(new BigDecimal(orderItem.getQuantity())));
        }

        //填充订单总金额
        OrderInfo orderInfo = orderInfoMapper.getOrderInfoByOrderId(orderId);
        BigDecimal oldAmount = orderInfo.getTotalAmount();
        if (oldAmount == null) {
            orderInfo.setTotalAmount(amount);
        } else {
            orderInfo.setTotalAmount(amount.add(oldAmount));
        }

        orderInfoMapper.updateOrder(orderInfo);

        return orderItemMapper.addOrderItems(orderItems);
    }
}
